/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isgesis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Agustin
 */
public class PersistenciaCliente {

    private static PersistenciaCliente instance;

    public static PersistenciaCliente getInstance() {
        if (instance == null) {
            instance = new PersistenciaCliente();
        }
        return instance;
    }

    public ResultSet ejecutarSeleccion(String query) throws SQLException {
        ResultSet rs;
        try {
            Conexion.getInstance().conectar();
            rs = Conexion.getInstance().select(query);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
        return rs;
    }

    public Cliente recorerRS(ResultSet rs) throws SQLException, ParseException {
        Cliente c1 = new Cliente();
        try {
            c1.setId(Integer.parseInt(rs.getObject(1).toString()));
            c1.setNombre(rs.getObject(2).toString());
            c1.setResponsable(rs.getObject(3).toString());
             ArrayList<Object> datos= new ArrayList<Object>();
            datos.add(c1.getId());
            datos.add("idCliente");
            c1.setTelefonos(PersistenciaTelefono.getInstance().selectTelefonos(datos));
            c1.setLugares(PersistenciaLugar.getInstance().selectLugares(datos));
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
        return c1;
    }

    public List<Cliente> traerRS(String query) throws ParseException {
        List<Cliente> clientes = new ArrayList<Cliente>();
        try {
            ResultSet rs = ejecutarSeleccion(query);
            if (rs.next()) {
                clientes.add(recorerRS(rs));
                while (rs.next()) {
                    clientes.add(recorerRS(rs));
                }
            } else {
                Cliente c = new Cliente();
                clientes.add(c);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        return clientes;
    }

    public List<Cliente> selectClientes(ArrayList<Object> datos) throws ParseException, SQLException {
       String query = "";
        if (datos.get(1) == "idCliente") {
            query = "SELECT * FROM clientes WHERE idCliente=" + datos.get(0);
        } else {
            query = "SELECT * FROM clientes WHERE nombre=" + datos.get(0);
        }
        return traerRS(query);
    }

    public void insert(Cliente cliente) throws ParseException {
        try {
           String query = "insert into clientes (nombre, responsable) values ('" + cliente.getNombre() + "','" + cliente.getResponsable() + "')";
            Conexion.getInstance().conectar();
            Conexion.getInstance().insert(query);
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
